package com.zkzx.company.api.service.message;

import com.zkzx.common.company.model.CompanyVO;
import com.zkzx.common.global.Global;
import com.zkzx.common.message.command.MessageCommand;
import com.zkzx.common.message.constants.MessageStatusEnum;
import com.zkzx.common.message.model.MessageListVO;
import com.zkzx.common.vo.PageResult;
import com.zkzx.common.vo.RequestFilterParams;
import com.zkzx.company.api.client.company.CompanyClient;
import com.zkzx.company.api.mapper.MessageMapper;
import com.zkzx.company.api.mapper.entity.Message;
import com.zkzx.company.api.mapper.entity.MessageExample;
import com.zkzx.util.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 北京中科智兴物联科技有限公司 @版权所有
 *
 * @Author: zhaoyihe@zkraising.com
 * @Date: 2021/05/17/17:07
 * @Description:
 */
@Slf4j
@Service
public class MessageService {

    @Resource
    private MessageMapper messageMapper;
    @Autowired
    private CompanyClient companyClient;


    public PageResult<MessageListVO> find(RequestFilterParams params,  Integer companyId)  {

        //根据企业id查电话
        CompanyVO companyVO = companyClient.getById(companyId);

        MessageExample messageExample = new MessageExample();
        MessageExample.Criteria criteria = messageExample.createCriteria();
        criteria.andSendidEqualTo(companyId);
        criteria.andSendmobileEqualTo(companyVO.getMemberMobile());

        long totalNum = messageMapper.countByExample(messageExample);

        if (totalNum <= Global.ZERO){
            return new PageResult<>(params.getPage(),params.getPerPage(), Collections.emptyList(), Global.ZERO);
        }

        if (StringUtils.isBlank(params.getOrder())){
            params.setOrder("DESC");
        }
        messageExample.setOrderByClause(params.getField().concat(" ").concat(params.getOrder()));
        messageExample.page(params.getPage()-Global.ONE , params.getPerPage());
        List<Message> messages = messageMapper.selectByExample(messageExample);

        List<MessageListVO> list=messages.stream().map(message -> MessageListVO
                .builder()
                .id(message.getId())
                .sendId(message.getSendid())
                .type(message.getType())
                .link(message.getLink())
                .context(message.getContext())
                .status(message.getStatus())
                .sendTime(DateUtil.getYesterday(message.getCreatetime()))
                .build()).collect(Collectors.toList());

        return new PageResult<>(params.getPage(),params.getPerPage(),list, (int) totalNum);

    }



    /**
     * 标记已读
     * @param companyId 企业id
     * @param userId 登录人id
     */
    public void reading(MessageCommand command,int userId, int companyId) {
        List<Integer> ids = command.getIds();
        if (CollectionUtils.isNotEmpty(ids)){
            ids.forEach(id->{
                MessageExample messageExample = new MessageExample();
                MessageExample.Criteria criteria = messageExample.createCriteria();
                criteria.andIdEqualTo(id);
                Message message = new Message();
                message.setLastmodifieduser(userId);
                message.setStatus(MessageStatusEnum.READ.name());
                messageMapper.updateByExampleSelective(message,messageExample);
            });

        }
    }


    /**
     * 查询未读站内信
     * @param companyId 企业id
     */
    public int getUnreadMessageNum(int companyId){
        MessageExample messageExample = new MessageExample();
        MessageExample.Criteria criteria = messageExample.createCriteria();
        criteria.andSendidEqualTo(companyId);
        criteria.andStatusEqualTo(MessageStatusEnum.SUC.name());
        long totalNum = messageMapper.countByExample(messageExample);
        return (int)totalNum;
    }

}
